﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using PhotoAlbums.Models;
using System.Data.Entity.Infrastructure;
using PhotoAlbums.Models.VO;

namespace PhotoAlbums.Controllers
{
    public class UserController : Controller
    {
        private PhotoAlbumsDB db = new PhotoAlbumsDB();

        /*
        * @GET: User/Login
        * @Doc: 用户登录
        */
        public ActionResult Login()
        {
            return View();
        }

        /*
        * @POST: User/Login
        * @Doc: 用户登录
        * @param Email 邮件号码
        * @param Password 密码
        */
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login([Bind(Include = "Email,Password")] LoginVO loginVO)
        {
            User user = null;

            DbQuery<User> dbQuery = db.User.Where(u => u.Email == loginVO.Email) as DbQuery<User>;
            List<User> users = dbQuery.ToList();

            if (users == null || users.Count != 1)
            {
                return View();
            }

            user = users[0];

            if (loginVO.Password.Equals(user.Password))
            {
                Session["id"] = user.Id;
                Session["username"] = user.Username;
                return RedirectToAction("Index", "Home");
            }

            return View();
        }

        /*
        * @GET: User/Register
        * @Doc: 用户注册
        */
        public ActionResult Register()
        {
            return View();
        }

        /*
        * @POST: User/Register
        * @Doc: 用户注册
        * @param Email 邮件号码
        * @param Password 密码
        */
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Register([Bind(Include = "Email,Password")] LoginVO loginVO)
        {

            DbQuery<User> dbQuery = db.User.Where(u => u.Email == loginVO.Email) as DbQuery<User>;
            List<User> users = dbQuery.ToList();

            if (users.Count == 0)
            {
                User user = new User();
                user.Email = loginVO.Email;
                user.Password = loginVO.Password;

                if (ModelState.IsValid)
                {
                    db.User.Add(user);
                    db.SaveChanges();
                    return RedirectToAction("Index", "Home");
                }
            }

            return View();
        }

        /*
        * @GET: User/Personal
        * @Doc: 个人中心
        */
        public ActionResult Personal()
        {
            // 未登录跳转
            if (Session["id"] == null)
            {
                return RedirectToAction("Login", "User");
            }

            return RedirectToAction("Index", "Catalog");
        }

        /*
        * @GET: User/Logout
        * @Doc: 退出登录
        */
        public ActionResult Logout()
        {
            Session["id"] = null;
            Session["username"] = null;

            return RedirectToAction("Login", "User");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
